/*
 * @lc app=leetcode.cn id=239 lang=javascript
 *
 * [239] 滑动窗口最大值
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
var maxSlidingWindow = function(nums, k) {
    let maxIndex = -1
    let resArr = []
    for (let index = 0; index < nums.length - k; index++) {
        const e1 = nums[index + k];
        const maxEle = nums[maxIndex]
        if (maxIndex >= (index + k)) {
            if (e1 > maxEle) {
                maxIndex = index + k
                resArr.push(e1)
            }
        } else {
            for (let addIndex = index; addIndex < index + k; addIndex++) {
                const e2 = nums[addIndex];
                if (!(nums[maxIndex] > e2)) {
                    maxIndex = addIndex
                }
            }
            resArr.push(nums[maxIndex])
        }
    }
    return resArr
};
// @lc code=end

